﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WpfAppDemo.DAL;
using WpfAppDemo.Model;

namespace WpfAppDemo.ViewModel
{
    /// <summary>
    ///
    /// </summary>
    public partial class MainViewModel : ObservableObject
    {
        #region 属性及构造函数

        private LocalDb localDb;

        [ObservableProperty]
        private List<Student>? gridModelList;

        /// <summary>
        /// 查询条件
        /// </summary>
        [ObservableProperty]
        private string? search;


        /// <summary>
        ///
        /// </summary>
        public MainViewModel()
        {
            localDb = new LocalDb();
        }

        #endregion

        [RelayCommand]
        public Task Query()
        {
            List<Student> students;
            if (string.IsNullOrEmpty(this.Search))
            {
                students = localDb.Query();
            }
            else
            {
                students = localDb.QueryByName(this.Search);
            }

            GridModelList = new List<Student>();
            if (students != null)
            {
                students.ForEach((t) =>
                {
                    GridModelList.Add(t);
                });
            }

            return Task.CompletedTask;
        }

        /// <summary>
        /// 重置
        /// </summary>
        [RelayCommand]
        public Task Reset()
        {
            this.Search = string.Empty;
            this.Query();
            return Task.CompletedTask;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="Id"></param>
        [RelayCommand]
        public Task Edit(int Id)
        {
            var model = localDb.QueryById(Id);
            if (model != null)
            {
                StudentWindow view = new StudentWindow(model);
                var r = view.ShowDialog();
                if (r.Value)
                {
                    var newModel = GridModelList.FirstOrDefault(t => t.Id == model.Id);
                    if (newModel != null)
                    {
                        newModel.Name = model.Name;
                        newModel.Age = model.Age;
                        newModel.Classes = model.Classes;
                    }
                    this.Query();
                }
            }
            return Task.CompletedTask;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        [RelayCommand]
        public Task Delete(int Id)
        {
            var model = localDb.QueryById(Id);
            if (model != null)
            {
                var r = MessageBox.Show($"确定要删除吗【{model.Name}】？", "提示", MessageBoxButton.YesNo);
                if (r == MessageBoxResult.Yes)
                {
                    localDb.DelStudent(Id);
                    this.Query();
                }
            }

            return Task.CompletedTask;
        }

        /// <summary>
        /// 新增
        /// </summary>
        [RelayCommand]
        public Task Add()
        {
            Student model = new Student();
            StudentWindow view = new StudentWindow(model);
            var r = view.ShowDialog();
            if (r.Value)
            {
                model.Id = GridModelList.Max(t => t.Id) + 1;
                localDb.AddStudent(model);
                this.Query();
            }

            return Task.CompletedTask;
        }
    }
}
